package com.itheima.web.controller.system;

import com.fasterxml.jackson.databind.ObjectMapper;
import com.github.pagehelper.PageInfo;
import com.itheima.domain.system.Role;
import com.itheima.utils.BeanUtil;
import com.itheima.web.BaseServlet.BaseServlet;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;
import java.util.Map;

@WebServlet("/system/role")
public class RoleServlet extends BaseServlet {
    /**
     * 分页查询的方法
     *
     * @param req
     * @param resp
     * @throws ServletException
     * @throws IOException
     */
    public void list(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        String size = req.getParameter("size");
        String page = req.getParameter("page");
        PageInfo<Role> all = roleService.findAll(size, page);
        req.setAttribute("page", all);
        req.getRequestDispatcher("/WEB-INF/pages/system/role/list.jsp").forward(req, resp);
    }

    /**
     * 跳转到添加界面
     *
     * @param req
     * @param resp
     * @throws ServletException
     * @throws IOException
     */
    public void toAdd(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        //加载所有的部门信息放入到roleList
        List<Role> all = roleService.findAll();
        req.setAttribute("roleList", all);
        //跳转到添加界面
        req.getRequestDispatcher("/WEB-INF/pages/system/role/add.jsp").forward(req, resp);
    }

    /**
     * 添加的方法
     *
     * @param req
     * @param resp
     * @throws IOException
     */
    public void save(HttpServletRequest req, HttpServletResponse resp) throws IOException {
        Role role = BeanUtil.fillBean(req, Role.class, "yyyy-MM-dd");
        roleService.save(role);
        //从定向到分页查询页面
        resp.sendRedirect(req.getContextPath() + "/system/role?operation=list");
    }

    /**
     * 跳转到修改界面
     *
     * @param req
     * @param resp
     * @throws ServletException
     * @throws IOException
     */
    public void toEdit(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        //首先获取到需要修改的信息
        String id = req.getParameter("id");
        Role role = roleService.findById(id);
        //请求转发到修改的jsp界面
        req.setAttribute("role", role);
        req.getRequestDispatcher("/WEB-INF/pages/system/role/update.jsp").forward(req, resp);
    }

    /**
     * 修改
     *
     * @param req
     * @param resp
     * @throws IOException
     */
    public void edit(HttpServletRequest req, HttpServletResponse resp) throws IOException {
        Role role = BeanUtil.fillBean(req, Role.class, "yyyy-MM-dd");
        roleService.update(role);
        //修改完之后重定向到分页查询界面
        resp.sendRedirect(req.getContextPath() + "/system/role?operation=list");
    }

    public void delete(HttpServletRequest req, HttpServletResponse resp) throws IOException {
        String id = req.getParameter("id");
        roleService.delete(id);
        //删除完毕之后重定向到分页查询界面
        resp.sendRedirect(req.getContextPath() + "/system/role?operation=list");
    }

    public void author(HttpServletRequest req, HttpServletResponse resp) throws IOException, ServletException {

        //1.获取要授权的角色id
        String roleId = req.getParameter("id");
        //根据id查询到对应的数据
        Role role = roleService.findById(roleId);
        req.setAttribute("role", role);
        //3.根据当前角色id查询所有的模块数据,并加载关系数据
        List<Map> map = moduleService.findAuthorDataByRoleId(roleId);
        ObjectMapper mapper = new ObjectMapper();
        String json = mapper.writeValueAsString(map);
       // new ObjectMapper().writeValue(resp.getWriter(),map);
        req.setAttribute("roleModuleJson",json);
        //TODO数据未查询
        //跳转到树页面中
        req.getRequestDispatcher("/WEB-INF/pages/system/role/author.jsp").forward(req,resp);
    }


}
